package cn.tedu.boottext31.controller;

import cn.tedu.boottext31.mapper.EmpMapper;
import cn.tedu.boottext31.vo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class EmpController {
    @Autowired(required = false)
    EmpMapper mapper;

   @RequestMapping("/insert")
    public String insert(Emp emp){
       System.out.println("emp = " + emp);
       mapper.insert(emp);
       return "添加完成!";
   }


   @RequestMapping("/select")
    public String select(){
       List<Emp> list = mapper.select();
       //将数据显示到表格中
       String html="<table border=1>";
       html+="<caption>员工列表</caption>";
       html+="<tr><th>id</th><th>名字</th><th>工作</th><th>工资</th><th>操作</th></tr>";
       //添加员工信息
       for (Emp emp : list) {//有几条信息取决于集合的长度
           html+="<tr>";
           html+="<td>"+emp.getId()+"</td>";
           html+="<td>"+emp.getName()+"</td>";
           html+="<td>"+emp.getJob()+"</td>";
           html+="<td>"+emp.getSal()+"</td>";
           html+="<td><a href='/delete?id="+emp.getId()+"'>删除</a></td>";
           html+="</tr>";
       }
       html+="</html>";
       return html;
   }





   @RequestMapping("/delete")
    public String delete(int id){
       mapper.delete( id);
       return "删除完毕<a href='/select'>放回查询列表</a>";
   }




@RequestMapping("/update")
    public String update(Emp emp){
       mapper.update(emp);
       return "修改完成!<a href='/select'>放回员工列表</a>";
}







}




















